package io.p2p.stream.stun.handler;

import com.alibaba.fastjson.JSONObject;
import io.netty.channel.ChannelHandlerContext;
import io.p2p.stream.stun.model.StunSharedModel;
import io.p2p.stream.stun.task.MainServerHeartbeatTask;
import io.p2p.stream.stun.util.UniqueIdGeneratorUtil;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;

@Slf4j
public class MainlineLoginAckHandler implements StunBaseHandler{

    public static final String commandType = "mainlineLoginAck";

    @Override
    public void process(ChannelHandlerContext ctx, InetSocketAddress remoteAddress,  JSONObject buffer) {
        boolean success = buffer.getBooleanValue("success");

        if(success){
            long workerId = buffer.getLong("workerId");
            long dataCenterId = buffer.getLong("dataCenterId");
            UniqueIdGeneratorUtil.getInstance().setWorkerIdAndDataCenterIdSequence(workerId,dataCenterId);

            ctx.executor().scheduleAtFixedRate(new MainServerHeartbeatTask(StunSharedModel.stunServerName,
                    StunSharedModel.mainServerFirstIP,
                    StunSharedModel.mainServerFirstPort,
                    ctx.channel()),0,5, TimeUnit.SECONDS);
        }
    }
}
